const app = getApp()
import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageBaseURL: app.imageBaseURL,
    picBaseURL:app.picBaseURL,
    fileList: [],//页面显示
    imgurl:[],//赋参
    submitData:{},
  },
  //上传图片
  async uploadPic(e){
    let that = this
    wx.uploadFile({
      url: this.data.picBaseURL, //仅为示例，非真实的接口地址
      filePath: e.detail.file.url,
      name: 'files[]',
      formData: {
        'user': 'test'
      },
      success (res){
        const obj = JSON.parse(res.data)
        let fileList = that.data.fileList
        let imgurl = that.data.imgurl
        fileList.push({url:that.data.imageBaseURL+'/' + obj[0].file})
        imgurl.push(obj[0].file)
        that.data.submitData['urlid[]'] = imgurl;
        that.setData({
          submitData:that.data.submitData,
          fileList,
          imgurl
        })
      }
    })
  },
  //删除图片
  delete(e){
    let ind = e.detail.index;
    this.data.fileList.splice(ind,1); 
    this.data.imgurl.splice(ind,1); 
    this.data.submitData['urlid[]'] = this.data.imgurl;
   this.setData({
    submitData:this.data.submitData,
    fileList:this.data.fileList,
    imgurl:this.data.imgurl
   })

  },
  //申请成立
  async apply(){
    let data= this.data.submitData;
    data['flag'] = 'social_organization_new'
    console.log(data)
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      Toast.success('申请成功');
      wx.switchTab({
        url: '/pages/my/index',
      })
    }else {
      Toast(message)
    }
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let submitdata = JSON.parse(options.all)

    let fileList = this.data.fileList
    let imgurl = this.data.imgurl
    if(submitdata['urlid[]'] != ''){
      submitdata['urlid[]'].forEach((item,index) => {
        fileList.push({url:this.data.imageBaseURL+'/' + item})
        imgurl.push(item)
      })
    }
    submitdata['urlid[]'] = imgurl
    this.setData({
      submitData:submitdata,
      fileList,
      imgurl
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})